package upload

import (
	"context"
	"fmt"
	"github.com/minio/minio-go/v7"
	"go.uber.org/zap"
	"io"
	"os"
	"ruoyi-go/config"
	"ruoyi-go/framework/dal"
	"ruoyi-go/framework/log"
)

var name string

func SavePathFile(path string) (string, string) {
	log.L.Info("Start to Save Path File", zap.String("path", path))
	file, err := os.Open(path)

	if err != nil {
		panic(err)
	}
	defer file.Close()
	s := file.Name()

	key := GetKey(s)

	PutFile(key, file)
	return key, s

}

func PutFile(key string, reader io.Reader) string {
	ctx := context.Background()
	info, err := dal.MinioClient.PutObject(ctx, name, key, reader, -1,
		minio.PutObjectOptions{ContentType: "application/octet-stream"})
	if err != nil {
		panic(err)
	}
	fmt.Println(info)
	//return info
	return key
}

// 小文件
func GetSmallFile(key string) *minio.Object {
	ctx := context.Background()
	object, err := dal.MinioClient.GetObject(ctx, name, key, minio.GetObjectOptions{})
	if err != nil {
		panic(err)
	}
	//defer object.Close()
	return object
}

func init() {
	name = config.Data.Minio.Bucket
}
